using NAudio;
using NAudio.Wave;
using NAudio.Vorbis;
using NVorbis.Contracts;


namespace 音乐播放
{
    public partial class Form1 : Form
    {

        string[] files;

        List<string> localmusicList = new List<string> { };
        public Form1()
        {
            InitializeComponent();
        }
        private void musicplay(string filename)
        {
            axWindowsMediaPlayer1.URL = filename;
            string extention = Path.GetExtension(filename);

            if (extention == ".ogg")
            {
                Console.WriteLine("这是ogg文件");
                listBox1.Items.Add(filename); // 将选中的ogg文件路径添加到列表框
                localmusicList.Add(filename);
            }
            else
            {
                axWindowsMediaPlayer1.Ctlcontrols.play();
            }

        }
        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "选择音频|*.mp3;*.flac;*.wav";
            openFileDialog1.Multiselect = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                localmusicList.Clear();
                listBox1.Items.Clear();//清理控件

                if (files != null)
                {
                    Array.Clear(files, 0, files.Length);
                }//清理文件

                files = openFileDialog1.FileNames;
                string[] arry = files;
                foreach (string x in arry)
                {
                    listBox1.Items.Add(x);
                    localmusicList.Add(x);
                }
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (localmusicList.Count > 0)
            {
                int selectedIndex = listBox1.SelectedIndex;
                if (selectedIndex >= 0)
                {
                    string selectedFile = localmusicList[selectedIndex];
                    if (!selectedFile.EndsWith(".ogg"))
                    {
                        axWindowsMediaPlayer1.URL = selectedFile;
                        musicplay(axWindowsMediaPlayer1.URL);
                        label1.Text = Path.GetFileNameWithoutExtension(selectedFile);
                    }
                }
            }
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            axWindowsMediaPlayer1.settings.volume = trackBar1.Value;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (localmusicList.Count > 0)
            {
                int index = listBox1.SelectedIndex + 1;

                if (index >= localmusicList.Count())
                {
                    index = 0;
                }
                axWindowsMediaPlayer1.URL = localmusicList[index];
                musicplay(axWindowsMediaPlayer1.URL);
                label1.Text = Path.GetFileNameWithoutExtension(localmusicList[index]);

                listBox1.SelectedIndex = index;
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFiledialog = new OpenFileDialog();
            openFiledialog.Filter = "打开音频|*.ogg";
            string oggFilePath = "";

            if (openFiledialog.ShowDialog() == DialogResult.OK)
            {
                oggFilePath = openFiledialog.FileName;
                listBox1.Items.Add(oggFilePath); // 将选择的ogg文件路径添加到列表框
                localmusicList.Add(oggFilePath); // 将选择的ogg文件路径添加到列表
                Task.Run(() =>
                {
                    using (var vorbis = new VorbisWaveReader(oggFilePath))
                    {
                        using (var outputDevice = new WaveOutEvent())
                        {
                            outputDevice.Init(vorbis);
                            outputDevice.Play();

                            // 等待播放结束，而不是使用Thread.Sleep进行轮询  
                            while (outputDevice.PlaybackState == PlaybackState.Playing)
                            {
                                Thread.Sleep(100); // 减少轮询频率以降低CPU使用率  
                            }
                        }
                    }
                });
            }
        }


    }
}

